<?php
class Model_suport extends MY_Model {

	function __construct() {
		parent::__construct();
		$this->aUser = $this->usersession->currentUser();
	}

	function getData($aPost) {

		$iStart = (isset($aPost["start"]) && ($aPost["start"] != "")) ? $aPost["start"] : 0;
		$iLimit = (isset($aPost["limit"]) && ($aPost["limit"] != "")) ? $aPost["limit"] : 100;
		$sSort = (isset($aPost["sort"]) && ($aPost["sort"] != "")) ? $aPost["sort"] : "suport_prioritate";
		$sDir = (isset($aPost["dir"]) && ($aPost["dir"] != "")) ? $aPost["dir"] : "ASC";
		$aFilters = isset($aPost["filter"]) ? json_decode($aPost["filter"]) : NULL;

		$aColumnMapping = array();

		$this->db->order_by($sSort, $sDir);
		$this->gridFilters($sSort, $sDir, $iStart, $iLimit, $aFilters, FALSE, $aColumnMapping);
		$this->db->limit($iLimit, $iStart);
		$query = $this->db->get("suport");

		$totalCount = $this->db->count_all_results("suport");
		$aData = $query->result_array();

		$data = array(
			'totalCount' => $totalCount,
			'data'       => $aData
		);

		return $data;
	}

	function operation($aData) {

		$aTemp = array(
			"suport_text"       => $aData["suport_text"],
			"suport_status"     => $aData["suport_status"],
			"suport_type"       => $aData["suport_type"],
			"suport_pret"       => $aData["suport_pret"],
			"suport_prioritate" => $aData["suport_prioritate"]
		);

		//insert
		if ($aData["suport_id"] == 0) {
			$aTemp["user_id"] = $this->aUser["user_id"];
			$this->db->insert("support", $aTemp);
		} else {
			$this->db->where("suport_id", $aData["suport_id"]);
			$this->db->update("suport", $aTemp);
		}
	}

	/**
	 * Functie care aduce datele despre o inregistrare
	 */
	function getRecord($aData) {
		$this->db->where("suport_id", $aData["iRec"]);
		$aResult = $this->db->get("suport")->row_array();

		return $aResult;
	}

}
